<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/8
 * Time: 17:29
 */
namespace  zhtfrontend\components\services\commission;


use yii\base\Component;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageGoods;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\MechanicSubsidy;
use zhtfrontend\models\datas\Orders;
use common\models\datas\CardAttr;
use zhtfrontend\models\datas\OrdersCommission;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\OrdersItems;
use zhtfrontend\models\datas\OrdersItemsRelation;


class MechanicCommission extends Component{

    /**
     * 2017-9-7
     * 1、现金支付是调用
     * 2、现金支付后，插入机构补贴表
     */
    public static function InsertoMechanicSubsidy($model)
    {
        $items = OrdersItems::find()->where(['orders_id'=>$model->id,'teach_type'=>1])->one();
        $goods = Goods::findOne($items->goods_id);
        $classGoods = ClassManageGoods::find()->where(['goods_id'=>$items->goods_id])->one();
        $classManage = ClassManage::find()->where(['class_id'=>$classGoods->class_id,'type'=>1])->one();
        $count = 0;
        $attr = GoodsAttr::find()->where(['goods_id'=>$goods->id,'obj_type'=>'class_count'])->one();
        $count = $attr->value;

//        $itemRelation = OrdersItemsRelation::find()->where(['orders_items_id'=>$items->id])->one();
//        if($itemRelation != null){
//
//        }

        $MechanicSubsidy = MechanicSubsidy::find()->where(['class_id'=>$classManage->class_id,'mechanic_id'=>$classManage->mechanic_id])->all();
        $lessonCost = round($goods->cost/$count,2); //每节课次的补贴
        if($MechanicSubsidy == NULL){
            for ($i= 1 ;$i <= $count;$i++){
                $MechanicSubsidyModel = new MechanicSubsidy();
                $MechanicSubsidyModel->mechanic_id = $classManage->mechanic_id;
                $MechanicSubsidyModel->class_id = $classManage->class_id;
                $MechanicSubsidyModel->lesson = $i;
                $MechanicSubsidyModel->people_number = 1;
                $MechanicSubsidyModel->amount = $lessonCost;
                $MechanicSubsidyModel->lesson_amount = $lessonCost;
                $MechanicSubsidyModel->type = $goods->subsidy_type;
                $MechanicSubsidyModel->goods_id = $goods->id;
                $MechanicSubsidyModel->save(false);
            }
        }else{
            if($goods->subsidy_type == 1){
                foreach ($MechanicSubsidy as $item){
                    $item->people_number += 1;
                    $item->amount += $lessonCost;
                    $item->save(false);
                }
            }
        }
    }

    /**
     * 预约课程推可是减少机构补贴
     * @param $goods_id //课程商品id
     * @param $lesson  //从此课次开始退
     */
    public static function InserRefund($goods_id,$lesson){
        $classGoods = ClassManageGoods::find()->where(['goods_id'=>$goods_id])->one();
        $classManage = ClassManage::findOne($classGoods->class_id);
        $MechanicSubsidy = MechanicSubsidy::find()->where(['class_id' => $classManage->class_id, 'mechanic_id' => $classManage->mechanic_id])->indexBy('lesson')->all();
        if ($MechanicSubsidy != null) {
            foreach ($MechanicSubsidy as $k => $item) {
                if ($k >= $lesson) {
                    $item->people_number -= 1;
                    $item->amount -= $item->lesson_amount;
                    $item->save(false);
                }
            }
        }

    }

}
